#include "../include/my_oled.hpp"

#define SLEEP_OLED_TIMES 3000

static U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R2, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ 1, /* data=*/ 2);
//  U8G2_SSD1306_128X32_WINSTAR_2_HW_I2C u8g2(U8G2_R0,/* reset=*/ U8X8_PIN_NONE, /* clock=*/ 1, /* data=*/ 2);

void InitOled(void)
{
   u8g2.begin();
   //u8g2.setContrast(10);//降低亮度
   u8g2.clearBuffer();					// clear the internal memory
   u8g2.setFont(u8g2_font_ncenR08_tf);
}

void SleepOled(void)
{
  // u8g2.clearBuffer();
  u8g2.sleepOff();
  my_oled.cfg->sleepTick = 0;
}

void loopOled(void)
{
  //自动息屏
  if(my_oled.cfg->sleepTick > 1)
  {
    my_oled.cfg->sleepTick--;

  }else if(my_oled.cfg->sleepTick == 1)
  {
    //清空显示
    u8g2.clearBuffer();
    u8g2.sleepOn(); //进入休眠
    my_oled.cfg->sleepTick = 0;
  }
}

void showIPOled(String IP)
{
  my_oled.cfg->sleepTick = SLEEP_OLED_TIMES; //亮屏30秒
  u8g2.sleepOff();  //退出休眠

  u8g2.drawStr(0,8,((String)"IP: "+IP).c_str());
  u8g2.sendBuffer();					// transfer internal memory to the display
}

static void showFilament(String Filament){
  u8g2.drawStr(0,20,((String)"Filament:     ").c_str());
  u8g2.sendBuffer();
  u8g2.drawStr(0,20,((String)"Filament: "+Filament).c_str());
  u8g2.sendBuffer();
}

static MY_OLED_CFG cfg = {
  .sleepTick = SLEEP_OLED_TIMES, //30s
};

static MY_OLED_OPS ops = {
    .init = InitOled,
    .sleep= SleepOled,
    .loop = loopOled,
    .showIP = showIPOled,
    .showFilament = showFilament,
};

MY_OLED my_oled={
  .ops = &ops,
  .cfg = &cfg,
};
